package com.example.excel.base;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.example.excel.bean.BaseBean;
import com.example.excel.config.Constants;
import com.example.excel.http.ApiException;
import com.example.excel.http.HttpManager;
import com.example.excel.utils.LogUtil;
import com.example.excel.utils.SharedPreferencesUtils;
import com.example.excel.utils.Utils;

import java.io.IOException;
import java.lang.ref.WeakReference;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public abstract class ICallback<T> implements Callback<BaseBean<T>> {

    private WeakReference<BaseViewModel> contextWeakReference;

    public ICallback(BaseViewModel view, boolean isShowDialog) {
        if (view != null) {
            contextWeakReference = new WeakReference<>(view);
            contextWeakReference.get().getTxtDialog().setValue("正在加载...");
        }
    }

    public ICallback(BaseViewModel view) {
       this(view, true);
    }

    @Override
    public void onResponse(@NonNull Call<BaseBean<T>> call, @NonNull Response<BaseBean<T>> response) {
        LogUtil.d("OkHttp", "-->http is onNext");
        ApiException apiException = null;
        if (response.isSuccessful()) {
            BaseBean<T> body = response.body();
            if (body != null) {
                int code = body.getCode();
                String msg = body.getMsg();
                if (code == 0) {
                    onSuccess(body.getData());
                    if (!TextUtils.isEmpty(body.getToken())) {
                        HttpManager.token = body.getToken();
                        SharedPreferencesUtils.setString(Constants.LOGIN_TOKEN, body.getToken());
                    }
                } else {
                    apiException = new ApiException(new Throwable(msg), code);
                }
            } else {
                apiException = new ApiException(new Throwable(), ApiException.ERROR.UNKNOWN);
            }
        } else {
            try {
                assert response.errorBody() != null;
                String string = response.errorBody().string();
                apiException = new ApiException(new Throwable(string), response.code());
            } catch (IOException e) {
                apiException = new ApiException(new Throwable(e.getMessage()), response.code());
            }
        }
        if (apiException != null) {
            onFailure(apiException);
        }
        if (contextWeakReference != null && contextWeakReference.get() != null) {
            contextWeakReference.get().getDialog().setValue(false);
        }
    }

    @Override
    public void onFailure(@NonNull Call<BaseBean<T>> call, @NonNull Throwable t) {
        ApiException ex = ApiException.handleException(t);
        onFailure(ex);
    }

    public void onFailure(ApiException e) {
        LogUtil.d("OkHttp", "ApiException err:" + e);
        onError(e);
        Utils.getInstance().showToast(e.getMessage());
        if (contextWeakReference != null && contextWeakReference.get() != null) {
            contextWeakReference.get().getDialog().setValue(false);
            assert e.getMessage() != null;
            if (e.getMessage().contains("token已过期")) {
                HttpManager.token = "";
                SharedPreferencesUtils.setString(Constants.LOGIN_TOKEN, "");
                contextWeakReference.get().getJumpLogin().setValue(true);
            }
        }
    }

    public abstract void onSuccess(T o);

    protected void onError(ApiException e) {
        LogUtil.d("OkHttp", "--> onError");
    }

}
